import 'package:flutter/material.dart';
import 'package:flutter_module/commom/data/const.dart';
import 'package:flutter_module/mqtt/Mqtt.dart';
import 'package:flutter_module/widget/buttun/IconTextButtun.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:oktoast/oktoast.dart';

import '../../DemoLocalizations.dart';

class RandomWords extends StatefulWidget {
  final List<ShowSettingItem> models;

  const RandomWords({Key key, this.models}) : super(key: key);

  @override
  RandomWordsState createState() => RandomWordsState();
}

class RandomWordsState extends State<RandomWords> {
  // 增大字体大小
  final _biggerFont = const TextStyle(fontSize: 18.0);

  @override
  void initState() {
    super.initState();
    widget.models.forEach((f) {
//      print(f.itemTitle+f.itemValue);      //循环打印
    });
  }

  @override
  Widget build(BuildContext context) {
      //const FloatingActionButtonDemo({Key key}) : super(key: key);
  final _floatingActionButton=FloatingActionButton(//漂浮动作按钮
    onPressed: (){},
  child: Icon(Icons.add),//图标
  backgroundColor: Colors.blue,//背景颜色
shape: BeveledRectangleBorder(//矩形边框
  borderRadius:BorderRadius.circular(10.0)//为30的时候会变成菱形
),
  );
  final _floatingActionButtonExtended=FloatingActionButton.extended(//带标签的漂浮动作按钮
    onPressed: (){onClick();}, 
  // icon:Icon(Icons.add),
  label: Text(DemoLocalizations.of(context).Restoresettings));

    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Setting View'),
        centerTitle: true,
        actions: <Widget>[
        ],

        
      ),
      // floatingActionButton: _floatingActionButton,
     floatingActionButton: _floatingActionButtonExtended,
      body:  _buildSuggestions()
      
      // SingleChildScrollView(
      //   child:  Column(
      //     children: <Widget>[
      //       // _buildSuggestions(),
      //       new Container(
      //         height: MediaQuery.of(context).size.height ,
      //         child: _buildSuggestions(),
      //       ),

      //     // MaterialButton(
      //     //   onPressed: onClick,color: Colors.lightBlue,
      //     //   child: new Text(DemoLocalizations.of(context).Restoresettings),
      //     // ),
            
      //     ],
      //   ),
      // ),
      
      
    );
  }

  // listView 列表
  Widget _buildSuggestions() {
    return new ListView.builder(
        padding:  EdgeInsets.only(
                            top: 2.0, bottom: 2.0, left: 10.0, right: 10.0),
        itemCount: widget.models.length,
        itemBuilder: (context, i) {
          return new Container(
        child: new Column(
          children: <Widget>[
            _buildRow(widget.models[i]),
            new Divider( height: 3.0,)
          ],
        ),
      );
          //if (i.isOdd) return new Divider();
          // return _buildRow(widget.models[i]);
        });

        
  }

  // listView 每一行的内容和样式
  Widget _buildRow(ShowSettingItem pair) {
    return new ListTile(
      
      title: new Text(
        pair.itemTitle + ':' + pair.itemValue,
        style: _biggerFont,
      ),
      // trailing: new Icon(
      //   Icons.favorite,
      //   color: Colors.red,
      // ),
    );
  }

  void onClick() {
    final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
    builder.addByte(0x48);
    builder.addByte(0x02);

    //9,7,5,5
    builder.addByte(8);
    builder.addByte(5);
    builder.addByte(8);
    builder.addByte(5);
    // 门一门二开门时间
    // 9,9,9,9
    builder.addByte(8);
    builder.addByte(5);
    builder.addByte(8);
    builder.addByte(5);
    // 4,9,9,48,33,
    builder.addByte(2);
    builder.addByte(2);
    builder.addByte(2);
    builder.addByte(33);
    builder.addByte(20);

    // 快开满开快关慢关
    //100,100,50,50
    builder.addByte(100);
    builder.addByte(50);
    builder.addByte(100);
    builder.addByte(50);

    // 开门刹车距离
    builder.addByte(5);
    builder.addByte(5);
    // 半开距离
    builder.addByte(3);
    // 开门力
    builder.addByte(3);
    builder.addByte(1);
    builder.addByte(3);
    builder.addByte(1);
    // 39H-3fh

    builder.addByte(3);
    builder.addByte(1);
    builder.addByte(3);
    builder.addByte(1);
    builder.addByte(3);
    builder.addByte(1);

    Mqtt.getInstance().publishMessage(builder);
    showToast(DemoLocalizations.of(context).Restoresettings+'ok');

    Future.delayed(const Duration(milliseconds: 200), () {
      // Here you can write your code
      setState(() {
        final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
        builder.addByte(72);
        builder.addByte(1);
        builder.addByte(10);
        builder.addByte(11);
        Mqtt.getInstance().publishMessage(builder);
      });
    });

    Future.delayed(const Duration(milliseconds: 400), () {
      // Here you can write your code
      setState(() {
        Navigator.pop(context);
        // Here you can write your code for open new view
      });
    });
  }
}
